package test;






import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class DebugView extends ViewPart {
	public static String ID=DebugView.class.getName();
	 

	/**
	 * The constructor.
	 */
	public DebugView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		GridData data=new GridData(GridData.FILL_BOTH);
		FigureCanvas canvas=new FigureCanvas(parent);
		
		System.out.println("p:"+parent.getSize());
		System.out.println("shell:"+parent.getShell().getClientArea());
	//	canvas.setBackground(ColorConstants.blue);
		//canvas.setLayoutData(data);
		
		Figure root=new Figure();
		canvas.setContents(root);
		
		root.setLayoutManager(new StackLayout());
		//ScrollPane sc=new ScrollPane();
		//sc.setSize(parent.getShell().getClientArea().width-29,0);
		//sc.setBackgroundColor(ColorConstants.orange);
		//root.add(sc);
		Panel sc=new Panel();
		sc.setBackgroundColor(ColorConstants.orange);
		root.add(sc);
		sc.setLayoutManager(new XYLayout());
		//sc.setLayoutManager(new ScrollPaneLayout());
		
		
		Panel p2=new Panel();
		
		//p2.setSize(320,20);
		p2.setLayoutManager(new CustomLayout(sc));
		for (int i = 0; i < 400; i++) {
			p2.add(new Label("label "+i));
		}
		root.add(p2);
		//sc.setContents(p2);
		
		canvas.addControlListener(new ResizeMonitor(sc));
		/*
		
	//	root.setBackgroundColor(ColorConstants.red);
		canvas.setContents(root);
		Panel p2=new Panel();
		p2.setBackgroundColor(ColorConstants.orange);
		
		p2.setLayoutManager(new FlowLayout());
		//root.add(p2);
		
		for(int i=0;i<120;i++){
		Label label=new Label("hello "+i);
		p2.add(label);
		}
		
		ScrollPane sc=new ScrollPane();
		Panel p3=new Panel();
		sc.setSize(100,100);
		sc.setContents(p3);
		p3.setLayoutManager(new XYLayout());
		p3.add(p2);
		//sc.setLayoutManager(new XYLayout());
		
		root.add(sc);
		
		canvas.addControlListener(new ResizeMonitor(p2));
		*/
	}
	
	public class CustomLayout implements LayoutManager{
	    Figure control;
	    public CustomLayout(Figure control){
	    	this.control=control;
	    	
	    }
		public Object getConstraint(IFigure child) {
			// TODO Auto-generated method stub
			return null;
		}

		public Dimension getMinimumSize(IFigure container, int wHint, int hHint) {
			System.out.println(container+" minimumsize:"+"whint="+wHint+" hHint="+hHint);
			System.out.println("parent:"+control.getSize());
			return getPreferredSize(container,wHint,hHint);
		}

		public Dimension getPreferredSize(IFigure container, int wHint, int hHint) {
			System.out.println(container+" preferedsize:"+"whint="+wHint+" hHint="+hHint);
			System.out.println("parent:"+control.getSize());
			layout(container);
			Figure figures[]=(Figure[]) container.getChildren().toArray(new Figure[0]);
			int my=0;
			for (int i = 0; i < figures.length; i++) {
				my=Math.max(my,figures[i].getLocation().y+figures[i].getSize().height);
				
			}
			System.out.println("my="+my);
			return new Dimension(control.getClientArea().x,my);
		}

		public void invalidate() {
			System.out.println("invalidate");
		}

		public void layout(IFigure container) {
			Figure figures[]=(Figure[]) container.getChildren().toArray(new Figure[0]);
			int space=5;
			int dx=0;
			int dy=0;
			int preHeight=0;
			if(control.getSize().width==0 && control.getSize().height==0){
				return ;
			}
			for (int i = 0; i < figures.length; i++) {
				Dimension d=figures[i].getPreferredSize();
				if(dx==0){
					figures[i].setLocation(new Point(dx,dy));
					figures[i].setSize(d.width,d.height);
					dx+=d.width+space;
					preHeight=d.height+space;
				}else{
					if(dx+d.width>control.getClientArea().width-16){//for scroll bar
						dx=0;
						dy+=preHeight;
					}
					figures[i].setLocation(new Point(dx,dy));
					figures[i].setSize(d.width,d.height);
					dx+=d.width+space;
					preHeight=d.height+space;
				}
				
			}
			int my=0;
			for (int i = 0; i < figures.length; i++) {
				my=Math.max(my,figures[i].getLocation().y+figures[i].getSize().height);
				
			}
			//control.revalidate();//is OK?
			//container.setSize(control.getClientArea().width,my);
		}

		public void remove(IFigure child) {
			System.out.println("remove");
		}

		public void setConstraint(IFigure child, Object constraint) {
			// TODO Auto-generated method stub
			
		}

		   
	   }
	public class ResizeMonitor implements ControlListener{
		Figure figure;
		public ResizeMonitor(Figure figure){
			this.figure=figure;
		}
		public void controlMoved(ControlEvent e) {
			
		}

		public void controlResized(ControlEvent e) {
			FigureCanvas source=(FigureCanvas)e.getSource();
			//System.out.println("xxxx-s");
			figure.revalidate();//i don't know.
			//source.pack();
			//System.out.println("xxxx-e");
		//	ScrollPane sc=(ScrollPane)figure;
			//sc.setViewport(new Viewport());
			//sc.revalidate();
		//	sc.getViewport().repaint();
			//sc.setScrollBarVisibility(sc.NEVER);
		//	sc.getViewport().revalidate();
			//Point pt=((Control)e.getSource()).getSize();
			//figure.setSize(pt.x,pt.y);
		}
		
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		
	}
}